package com.vogue.supplymall.admin.business.controller;

import com.vogue.supplymall.admin.business.domain.service.AdminDictService;
import com.vogue.supplymall.common.domain.dto.ControllerResultInfo;
import com.vogue.supplymall.common.exception.ParamErrorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * The type Common utils controller.
 *
 * @aouthor: zhangliang
 * @date: Create in 2018-05-07
 */
@RestController
public class CommonUtilsController extends BaseController {
    /**
     * The constant log.
     */
    public static final Logger LOGGER_COMMONUTILSCONTROLLER = LoggerFactory.getLogger(AdminHomePageController.class);
    @Resource
    private AdminDictService adminDictService;

    /**
     * @param appver appver
     * @param type type
     * @return ResponseEntity
     */
    @RequestMapping(value = "/is/{appver}/admin/business/dict/get/{type}", method = RequestMethod.POST)
    public ResponseEntity<Object> getDict(@PathVariable("appver") String appver, @PathVariable("type") String type) {
        LOGGER_COMMONUTILSCONTROLLER.info("get dict start >>>>>> ");
        if (null == type) {
            LOGGER_COMMONUTILSCONTROLLER.info("parameter error");
            throw new ParamErrorException("");
        }

        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setResult(true);
        resultInfo.setData(adminDictService.findDict(new Integer(type)));
        LOGGER_COMMONUTILSCONTROLLER.info(" <<<<<< get dict end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }

    /**
     * Gets open organ list.
     * 根据类型查询字典，{type}为前端传入的参数
     *
     * @param appver the appver
     * @param type   the type
     * @param keyword   the keyword
     * @return the open organ list
     */
    @RequestMapping(value = "/is/{appver}/admin/business/dict/get/{type}/{keyword}", method = RequestMethod.POST)
    public ResponseEntity<Object> getDictWithKey(@PathVariable("appver") String appver, @PathVariable("type") String type,
                                                 @PathVariable("keyword") String keyword) {
        LOGGER_COMMONUTILSCONTROLLER.info("get Dict With Key start >>>>>> ");
        if (null == type) {
            LOGGER_COMMONUTILSCONTROLLER.info("parameter error");
            throw new ParamErrorException("");
        }

        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setResult(true);
        resultInfo.setData(adminDictService.findDict(new Integer(type), keyword));
        LOGGER_COMMONUTILSCONTROLLER.info(" <<<<<< get Dict With Key  end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }
}
